/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.Customer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.Timer;

/**
 *
 * @author SIX-14
 */
public class Monitor implements Serializable{

    public Monitor(String[] process){
        this.process=new ProcessMonitor[process.length];
        for(int i=0; i<process.length; i++){
            this.process[i]= new ProcessMonitor(process[i]);
         }
        this.aplication=process;

    }
    public Monitor(){
    }

    private ProcessMonitor[] process;
    private String[] aplication;
    private int duration;
    private Timer timer  = new Timer (1000, new ActionListener ()
    {
    public void actionPerformed(ActionEvent e)
    {
          List<String> processes = new ArrayList<String>();
          try {
            String line;
            Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
            BufferedReader input = new BufferedReader
                (new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                if (!line.trim().equals("")) {
              // keep only the process name
              //System.out.println(line);//hasta aca me interesa/------------------************
                    line = line.substring(1);
                    processes.add(line.substring(0, line.indexOf("\"")));
                    //System.out.println(line.substring(0, line.indexOf("\"")));//////////********************
                }

            }
            input.close();
            }
            catch (Exception err) {
                err.printStackTrace();
            }
          String[] table= null;
          String aplication;
          boolean token=false;
          Iterator<String> it = processes.iterator();
          while (it.hasNext()) {
                 aplication = it.next();
                 for(int i=0; i<getAplication().length; i++){
                     if(aplication.equals(getAplication()[i])){
                         if(table==null){
                             table=new String[1];
                             table[0]=aplication;
                         }else{
                             for(int j=0; j<table.length; j++){
                                 if(aplication.equals(table[j])){
                                     token=true;
                                     break;
                                 }
                             }
                             if(!token){
                                 String[] table2= new String[table.length+1];
                                 for(int j=0; j<table.length; j++){
                                     table2[j]=table[j];
                                 }
                                 table2[table.length]=aplication;
                                 table=table2;
                                 token=false;
                             }
                         }
                     }
                 }
            }
          if(table!=null){
           for(int i=0; i<getProcess().length; i++){
            for(int j=0; j<table.length;j++){
                if(getProcess()[i].getName().equals(table[j])){
                    getProcess()[i].setDuration(getProcess()[i].getDuration()+1);
                    break;
                }
            }

          }
        }
        setDuration(getDuration()+1);

     }

});

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }



    public String[] getAplication() {
        return aplication;
    }

    public void setAplication(String[] aplication) {
        this.aplication = aplication;
    }



    public ProcessMonitor[] getProcess() {
        return process;
    }

    public void setProcess(ProcessMonitor[] process) {
        this.process = process;
    }

    public Timer getTimer() {
        return timer;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }

    public void beginMonitor(String[] process){
        this.process=new ProcessMonitor[process.length];
        for(int i=0; i<process.length; i++){
            this.process[i]= new ProcessMonitor(process[i]);
         }
        this.aplication=process;

        getTimer().start();
    }

    public ProcessMonitor[] finishMonitor(){
        getTimer().stop();
        ProcessMonitor[] process1=null;
        for(int i=0; i<getProcess().length; i++){
            if(getProcess()[i].getDuration()>0){
                if(process1==null){
                    process1=new ProcessMonitor[1];
                    process1[0]=getProcess()[i];
                }else{
                    ProcessMonitor[] process2=new ProcessMonitor[process1.length+1];
                    for( int j=0; j<process1.length; j++){
                            process2[j]=process1[j];
                    }
                    process2[process1.length]=getProcess()[i];
                    process1=process2;
                }
            }
        }
        /*if(process1!=null){
            for(int i=0; i<process1.length; i++){
                process1[i].setDuration(process1[i].getDuration());/////60
            }
        }*/
        return process1;
    }

    public void reset(){
        for(int i=0; i<getProcess().length; i++){
            getProcess()[i].setDuration(0);
        }
    }


}
